<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Interact.js 完整功能示例 - 修复版</title>
    <script src="https://unpkg.com/interactjs/dist/interact.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2980, #26d0ce);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            color: #333;
        }
        
        .container {
            max-width: 1200px;
            width: 95%;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.25);
            overflow: hidden;
            padding: 30px;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        header h1 {
            font-size: 2.8rem;
            color: #2c3e50;
            margin-bottom: 10px;
            text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
        }
        
        header p {
            color: #7f8c8d;
            font-size: 1.2rem;
            max-width: 800px;
            margin: 0 auto;
            line-height: 1.6;
        }
        
        .features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 25px;
            margin-bottom: 40px;
        }
        
        .feature-card {
            background: #ffffff;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border-left: 5px solid #3498db;
        }
        
        .feature-card h3 {
            color: #2980b9;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.5rem;
        }
        
        .feature-card p {
            color: #555;
            line-height: 1.7;
        }
        
        .feature-card:nth-child(2) {
            border-left-color: #9b59b6;
        }
        
        .feature-card:nth-child(3) {
            border-left-color: #2ecc71;
        }
        
        .feature-card:nth-child(2) h3 {
            color: #9b59b6;
        }
        
        .feature-card:nth-child(3) h3 {
            color: #2ecc71;
        }
        
        .demo-area {
            display: grid;
            grid-template-columns: 250px 1fr;
            gap: 25px;
            margin-bottom: 30px;
        }
        
        @media (max-width: 900px) {
            .demo-area {
                grid-template-columns: 1fr;
            }
        }
        
        .controls {
            background: #f8f9fb;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }
        
        .control-group {
            margin-bottom: 25px;
        }
        
        .control-group h3 {
            font-size: 1.3rem;
            color: #2c3e50;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #e0e7ff;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .control-item {
            margin-bottom: 15px;
            padding: 12px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        }
        
        label {
            display: flex;
            align-items: center;
            gap: 10px;
            cursor: pointer;
            font-weight: 500;
            color: #2c3e50;
        }
        
        input[type="checkbox"] {
            width: 18px;
            height: 18px;
            cursor: pointer;
        }
        
        input[type="range"] {
            width: 100%;
            cursor: pointer;
        }
        
        .canvas-container {
            position: relative;
            background: white;
            border-radius: 15px;
            height: 550px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            border: 1px solid #e0e7ff;
        }
        
        #interaction-canvas {
            width: 100%;
            height: 100%;
            background-color: #f5f7ff;
            position: relative;
        }
        
        .element {
            position: absolute;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            color: white;
            cursor: move;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
            transition: box-shadow 0.3s ease;
            border: 3px solid white;
        }
        
        .element:hover {
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        }
        
        .element1 {
            background: linear-gradient(45deg, #3498db, #2980b9);
            width: 150px;
            height: 150px;
            border-radius: 20px;
            top: 60px;
            left: 120px;
        }
        
        .element2 {
            background: linear-gradient(45deg, #e74c3c, #c0392b);
            width: 180px;
            height: 120px;
            border-radius: 10px;
            top: 150px;
            left: 400px;
        }
        
        .element3 {
            background: linear-gradient(45deg, #2ecc71, #27ae60);
            width: 140px;
            height: 140px;
            border-radius: 50%;
            top: 300px;
            left: 200px;
        }
        
        .restriction-area {
            position: absolute;
            border: 2px dashed #9b59b6;
            border-radius: 15px;
            background: rgba(155, 89, 182, 0.05);
        }
        
        .grid-lines {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-size: 20px 20px;
            background-image:
                linear-gradient(to right, rgba(200, 200, 200, 0.2) 1px, transparent 1px),
                linear-gradient(to bottom, rgba(200, 200, 200, 0.2) 1px, transparent 1px);
        }
        
        .event-log {
            background: #2c3e50;
            border-radius: 12px;
            padding: 20px;
            color: white;
            height: 200px;
            overflow-y: auto;
            font-family: 'Consolas', monospace;
            font-size: 0.9rem;
            box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.3);
        }
        
        .event-log h3 {
            margin-bottom: 15px;
            color: #3498db;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .log-entry {
            margin-bottom: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .log-timestamp {
            color: #7f8c8d;
            font-size: 0.8rem;
            margin-bottom: 3px;
        }
        
        footer {
            text-align: center;
            margin-top: 30px;
            color: #7f8c8d;
            padding-top: 20px;
            border-top: 1px solid #eee;
        }
        
        .badge {
            display: inline-block;
            background: #3498db;
            color: white;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 0.8rem;
            margin-right: 5px;
        }
        
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #3498db;
            color: white;
            border-radius: 30px;
            text-decoration: none;
            font-weight: bold;
            transition: all 0.3s ease;
            margin-top: 10px;
            border: none;
            cursor: pointer;
            box-shadow: 0 4px 10px rgba(52, 152, 219, 0.3);
        }
        
        .btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(52, 152, 219, 0.4);
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1><i class="fas fa-hand-pointer"></i> Interact.js 完整功能示例 - 修复版</h1>
            <p>修复了 "Uncaught TypeError: this.scope.interactables.get is not a function" 错误</p>
        </header>
        
        <div class="features">
            <div class="feature-card">
                <h3><i class="fas fa-arrows-alt"></i> 拖拽功能</h3>
                <p>可配置的拖拽功能，支持惯性、限制区域、约束轴、网格对齐和自定义手柄。</p>
            </div>
            <div class="feature-card">
                <h3><i class="fas fa-expand-alt"></i> 缩放功能</h3>
                <p>允许用户通过拖动手柄来调整元素大小，支持设置最小/最大尺寸比例。</p>
            </div>
            <div class="feature-card">
                <h3><i class="fas fa-sync-alt"></i> 旋转功能</h3>
                <p>用户可以通过旋转手柄改变元素角度，支持限制旋转范围和角度捕捉。</p>
            </div>
        </div>
        
        <div class="demo-area">
            <div class="controls">
                <div class="control-group">
                    <h3><i class="fas fa-sliders-h"></i> 交互设置</h3>
                    
                    <div class="control-item">
                        <label>
                            <input type="checkbox" id="inertia-toggle" checked>
                            惯性效果
                        </label>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            <input type="checkbox" id="grid-toggle">
                            网格对齐 (20px)
                        </label>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            <input type="checkbox" id="restrict-area">
                            限制在区域内
                        </label>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            <input type="checkbox" id="resize-toggle" checked>
                            启用缩放
                        </label>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            <input type="checkbox" id="rotate-toggle" checked>
                            启用旋转
                        </label>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            惯性强度: 
                            <span id="inertia-value">0.5</span>
                        </label>
                        <input type="range" id="inertia-range" min="0" max="1" step="0.1" value="0.5">
                    </div>
                    
                    <button class="btn" id="reset-btn"><i class="fas fa-redo"></i> 重置所有元素</button>
                </div>
                
                <div class="control-group">
                    <h3><i class="fas fa-sync-alt"></i> 旋转配置</h3>
                    
                    <div class="control-item">
                        <label>
                            角度: <span id="angle-value">0</span>°
                        </label>
                        <input type="range" id="angle-range" min="0" max="360" step="1" value="0" disabled>
                    </div>
                    
                    <div class="control-item">
                        <label>
                            角度增量: <span id="angle-increment-value">15</span>°
                        </label>
                        <input type="range" id="angle-increment-range" min="0" max="90" step="15" value="15">
                    </div>
                </div>
            </div>
            
            <div class="canvas-container">
                <div id="interaction-canvas">
                    <!-- Grid lines will be added by JS -->
                    <div class="restriction-area" id="restriction-area" style="display: none;"></div>
                    <div class="element element1" data-type="element" id="element1">
                        <i class="fas fa-arrows-alt" style="font-size: 50px;"></i>
                    </div>
                    <div class="element element2" data-type="element" id="element2">
                        <i class="fas fa-expand-alt" style="font-size: 50px;"></i>
                    </div>
                    <div class="element element3" data-type="element" id="element3">
                        <i class="fas fa-sync-alt" style="font-size: 50px;"></i>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="event-log">
            <h3><i class="fas fa-list"></i> 事件日志</h3>
            <div id="log-entries"></div>
        </div>
        
        <footer>
            <p><span class="badge">修复版</span> 此版本修复了 "Uncaught TypeError: this.scope.interactables.get is not a function" 错误</p>
            <p>&copy; 2023 Interact.js 示例 | 修复版</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 状态变量
            const state = {
                inertia: true,
                inertiaStrength: 0.5,
                grid: false,
                restrictedArea: false,
                resizeEnabled: true,
                rotateEnabled: true,
                angleIncrement: 15
            };
            
            // 事件日志功能
            const logEntries = document.getElementById('log-entries');
            let logId = 1;
            
            function logEvent(message, element = null) {
                const now = new Date();
                const timeString = now.toLocaleTimeString();
                const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
                
                const logEntry = document.createElement('div');
                logEntry.className = 'log-entry';
                
                // 如果指定了元素，在日志中添加元素信息
                const elementInfo = element ? ` (${element.id})` : '';
                
                logEntry.innerHTML = `
                    <div class="log-timestamp">[${timeString}.${milliseconds}]</div>
                    <div><span class="badge">${logId++}</span>${message}${elementInfo}</div>
                `;
                
                logEntries.prepend(logEntry);
                
                // 限制日志数量
                if (logEntries.children.length > 10) {
                    logEntries.removeChild(logEntries.lastChild);
                }
            }
            
            // 初始化日志
            logEvent("Interact.js 已初始化");
            logEvent("修复了 'Uncaught TypeError' 错误");
            
            // 初始化限制区域
            const area = document.getElementById('restriction-area');
            const canvas = document.getElementById('interaction-canvas');
            
            // 限制区域配置
            const restriction = {
                restriction: canvas,
                elementRect: { top: 0, left: 0, bottom: 1, right: 1 },
                endOnly: false
            };
            
            // 应用限制区域函数
            function updateRestriction() {
                const rect = canvas.getBoundingClientRect();
                const padding = 20;
                
                area.style.display = state.restrictedArea ? 'block' : 'none';
                area.style.top = padding + 'px';
                area.style.left = padding + 'px';
                area.style.width = (rect.width - 2 * padding) + 'px';
                area.style.height = (rect.height - 2 * padding) + 'px';
                
                restriction.restriction = state.restrictedArea ? area : canvas;
            }
            
            // 设置交互元素
            const elements = document.querySelectorAll('[data-type="element"]');
            
            // 存储交互实例
            const interactInstances = {};
            
            // 设置交互选项
            function setupInteractions() {
                // 创建网格线
                const gridLines = document.querySelector('.grid-lines');
                if (gridLines) gridLines.remove();
                
                if (state.grid) {
                    const grid = document.createElement('div');
                    grid.className = 'grid-lines';
                    canvas.appendChild(grid);
                }
                
                // 为每个元素初始化
                elements.forEach(element => {
                    const id = element.id;
                    
                    // 如果已经存在交互实例，先销毁
                    if (interactInstances[id]) {
                        interactInstances[id].unset();
                        delete interactInstances[id];
                    }
                    
                    const width = element.offsetWidth;
                    const height = element.offsetHeight;
                    
                    // 创建新的交互实例
                    const interactable = interact(element);
                    interactInstances[id] = interactable;
                    
                    // 拖拽配置
                    interactable.draggable({
                        inertia: state.inertia && {
                            resistance: 15,
                            minSpeed: 100,
                            endSpeed: 10 * state.inertiaStrength
                        },
                        modifiers: [
                            interact.modifiers.restrictRect({
                                restriction: state.restrictedArea ? area : canvas,
                                endOnly: false
                            }),
                            state.grid && interact.modifiers.snap({
                                targets: [
                                    interact.snappers.grid({ x: 20, y: 20 })
                                ],
                                range: Infinity,
                                relativePoints: [ { x: 0, y: 0 } ]
                            })
                        ].filter(Boolean),
                        autoScroll: true,
                        listeners: {
                            start: function(event) {
                                logEvent("开始拖拽", event.target);
                                event.target.classList.add('dragging');
                                event.target.style.zIndex = '10';
                            },
                            move: function(event) {
                                const target = event.target;
                                const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
                                const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
                                
                                target.style.transform = `translate(${x}px, ${y}px)`;
                                
                                target.setAttribute('data-x', x);
                                target.setAttribute('data-y', y);
                                
                                // 记录位置
                                logEvent(`拖拽移动 x:${x.toFixed(1)}, y:${y.toFixed(1)}`, event.target);
                            },
                            end: function(event) {
                                logEvent("结束拖拽", event.target);
                                event.target.classList.remove('dragging');
                                setTimeout(() => { event.target.style.zIndex = '1'; }, 1000);
                            }
                        }
                    });
                    
                    // 缩放配置
                    if (state.resizeEnabled) {
                        interactable.resizable({
                            edges: { left: true, right: true, bottom: true, top: true },
                            inertia: state.inertia,
                            modifiers: [
                                interact.modifiers.restrictSize({
                                    min: { width: 50, height: 50 }
                                })
                            ],
                            listeners: {
                                start: function(event) {
                                    logEvent("开始缩放", event.target);
                                },
                                move: function(event) {
                                    let { x, y } = event.target.dataset;
                                    x = parseFloat(x) || 0;
                                    y = parseFloat(y) || 0;
                                    
                                    // 更新元素大小
                                    event.target.style.width = event.rect.width + 'px';
                                    event.target.style.height = event.rect.height + 'px';
                                    
                                    // 更新元素位置
                                    x += event.deltaRect.left;
                                    y += event.deltaRect.top;

                                    
                                    
                                    event.target.style.transform = `translate(${x}px, ${y}px)`;
                                    
                                    event.target.setAttribute('data-x', x);
                                    event.target.setAttribute('data-y', y);
                                    
                                    logEvent(`缩放尺寸 w:${event.rect.width}, h:${event.rect.height}`, event.target);
                                },
                                end: function(event) {
                                    logEvent("结束缩放", event.target);
                                }
                            }
                        });
                    }
                    
                    // 旋转配置
                    if (state.rotateEnabled) {
                        interactable.gesturable({
                            listeners: {
                                start: function(event) {
                                    logEvent("开始旋转", event.target);
                                },
                                move: function(event) {
                                    const target = event.target;
                                    const angleDelta = state.angleIncrement > 0 ? 
                                        Math.round(event.angle / state.angleIncrement) * state.angleIncrement : 
                                        event.angle;
                                    
                                    const rotation = parseFloat(target.getAttribute('data-rotation')) || 0;
                                    target.style.transform = `translate(${target.dataset.x || 0}px, ${target.dataset.y || 0}px) rotate(${rotation + angleDelta}deg)`;
                                    target.setAttribute('data-rotation', rotation + angleDelta);
                                    
                                    // 更新滑块
                                    document.getElementById('angle-value').textContent = 
                                        (rotation + angleDelta).toFixed(0);
                                    
                                    logEvent(`旋转角度: ${(rotation + angleDelta).toFixed(1)}°`, event.target);
                                },
                                end: function(event) {
                                    logEvent("结束旋转", event.target);
                                }
                            }
                        });
                    }
                });
                
                logEvent("已更新交互设置");
            }
            
            // 初始化交互
            setupInteractions();
            
            // 控制面板事件处理
            document.getElementById('inertia-toggle').addEventListener('change', function() {
                state.inertia = this.checked;
                setupInteractions();
            });
            
            document.getElementById('grid-toggle').addEventListener('change', function() {
                state.grid = this.checked;
                setupInteractions();
            });
            
            document.getElementById('restrict-area').addEventListener('change', function() {
                state.restrictedArea = this.checked;
                updateRestriction();
                setupInteractions();
            });
            
            document.getElementById('resize-toggle').addEventListener('change', function() {
                state.resizeEnabled = this.checked;
                setupInteractions();
            });
            
            document.getElementById('rotate-toggle').addEventListener('change', function() {
                state.rotateEnabled = this.checked;
                setupInteractions();
            });
            
            document.getElementById('inertia-range').addEventListener('input', function() {
                state.inertiaStrength = parseFloat(this.value);
                document.getElementById('inertia-value').textContent = this.value;
                if (state.inertia) {
                    setupInteractions();
                }
            });
            
            document.getElementById('angle-increment-range').addEventListener('input', function() {
                state.angleIncrement = parseInt(this.value);
                document.getElementById('angle-increment-value').textContent = this.value;
                setupInteractions();
            });
            
            document.getElementById('reset-btn').addEventListener('click', function() {
                elements.forEach(element => {
                    element.removeAttribute('data-x');
                    element.removeAttribute('data-y');
                    element.removeAttribute('data-rotation');
                    element.style.transform = '';
                    element.style.width = '';
                    element.style.height = '';
                    
                    // 重置特定元素的尺寸
                    if (element.id === 'element1') {
                        element.style.width = '150px';
                        element.style.height = '150px';
                    } else if (element.id === 'element2') {
                        element.style.width = '180px';
                        element.style.height = '120px';
                    } else if (element.id === 'element3') {
                        element.style.width = '140px';
                        element.style.height = '140px';
                    }
                });
                
                document.getElementById('angle-value').textContent = '0';
                document.getElementById('angle-range').value = '0';
                
                logEvent("所有元素已重置");
                setupInteractions();
            });
            
            // 旋转滑块事件
            document.getElementById('angle-range').addEventListener('input', function() {
                const angle = parseInt(this.value);
                document.getElementById('angle-value').textContent = angle;
                
                elements.forEach(element => {
                    if (element.classList.contains('dragging')) {
                        return;
                    }
                    
                    const x = parseFloat(element.getAttribute('data-x')) || 0;
                    const y = parseFloat(element.getAttribute('data-y')) || 0;
                    
                    element.setAttribute('data-rotation', angle);
                    element.style.transform = `translate(${x}px, ${y}px) rotate(${angle}deg)`;
                });
            });
            
            // 添加指示
            logEvent("将以下interact.js功能添加到您的网页:");
            logEvent("• 拖拽带惯性效果");
            logEvent("• 缩放带最小尺寸限制");
            logEvent("• 通过手势实现平滑旋转");
        });
    </script>
</body>
</html>